package grafica;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;

/**
 * A sprite to be displayed on the screen. Note that a sprite
 * contains no state information, i.e. its just the image and 
 * not the location. This allows us to use a single sprite in
 * lots of different places without having to store multiple 
 * copies of the image.
 * 
 * @author Kevin Glass http://www.cokeandcode.com/
 */
public class Sprite {
	/** The image to be drawn for this sprite */
	private Image image;
	
	/**
	 * Create a new sprite based on an image
	 * 
	 * @param image The image that is this sprite
	 */
	public Sprite(Image image) {
		this.image = image;
	}
	
	/**
	 * Get the width of the drawn sprite
	 * 
	 * @return The width in pixels of this sprite
	 */
	public int getWidth() {
		return image.getWidth(null);
	}

	/**
	 * Get the height of the drawn sprite
	 * 
	 * @return The height in pixels of this sprite
	 */
	public int getHeight() {
		return image.getHeight(null);
	}
	
	/**
	 * Draw the sprite onto the graphics context provided
	 * 
	 * @param g The graphics context on which to draw the sprite
	 * @param x The x location at which to draw the sprite
	 * @param y The y location at which to draw the sprite
	 */
	public void draw(Graphics g,int x,int y) {
		g.drawImage(image,x,y, null);
	}
	
	/**
	 * Basado en el codigo publicado en la siguiente pagina:
	 * http://stackoverflow.com/questions/8639567/java-rotating-images
	 * @param g
	 * @param x
	 * @param y
	 * @param angle
	 */
	public void drawRotated(Graphics g, int x, int y, double angle){
        // The required drawing location
        int drawLocationX = x;
        int drawLocationY = y;

        // Rotation information
        double rotationRequired = Math.toRadians(angle);
        double locationX = image.getWidth(null) / 2;
        double locationY = image.getHeight(null) / 2;
        AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);

        // Drawing the rotated image at the required drawing locations
        g.drawImage(op.filter((BufferedImage) image, null), drawLocationX, drawLocationY, null);
	}
	
	/**
	 * Cambia el color de una imagen con extension png. 
	 * 
	 * http://stackoverflow.com/questions/16054596/change-color-of-non-transparent-parts-of-png-in-java
	 * @param r
	 * @param g
	 * @param b
	 */
	public void colorImage(int r, int g, int b) {
        int width = image.getWidth(null);
        int height = image.getHeight(null);
        WritableRaster raster = ((BufferedImage) image).getRaster();

        for (int xx = 0; xx < width; xx++) {
            for (int yy = 0; yy < height; yy++) {
                int[] pixels = raster.getPixel(xx, yy, (int[]) null);
                pixels[0] = r;
                pixels[1] = g;
                pixels[2] = b;
                raster.setPixel(xx, yy, pixels);
            }
        }
	}

}